<!--  -->
<template>
  <div class="">
    <heads></heads>
    <main>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="店铺名称">
                <span>{{ props.row.businessName }}</span>
              </el-form-item>
              <el-form-item label="店铺地址">
                <span>{{ props.row.businessAddress }}</span>
              </el-form-item>
              <el-form-item label="店铺介绍">
                <span>{{ props.row.businessText }}</span>
              </el-form-item>
              <el-form-item label="店铺 ID">
                <span>{{ props.row.id }}</span>
              </el-form-item>
              <el-form-item label="联系电话">
                <span>{{ props.row.businessPhone }}</span>
              </el-form-item>
              <el-form-item label="评分">
                <span>{{ props.row.businessScore }}</span>
              </el-form-item>
              <el-form-item label="销售量">
                <span>{{ props.row.busOrderNum }}</span>
              </el-form-item>
              <el-form-item label="分类">
                <span>{{ props.row.busClass.label + "/" + props.row.sonClass.label }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="店铺名称" prop="businessName"> </el-table-column>
        <el-table-column label="店铺地址" prop="businessAddress"> </el-table-column>
        <el-table-column label="店铺介绍" prop="businessText"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="uodateBus(scope.row.id)">编辑</el-button>
            <el-button size="mini" @click="addFoods(scope.row.id)">添加食品</el-button>
            <el-button type="danger" size="mini" @click="deleteBus(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </main>
    <footer>
      <el-pagination background layout="prev, pager, next" v-on:current-change="updatePage" v-on:prev-click="reducePage" v-on:next-click="addPage" :total="pageSum * 10"> </el-pagination>
    </footer>
    <el-dialog title="修改店铺信息" :visible.sync="dialogFormVisible">
      <el-form :model="business">
        <el-form-item label="店铺名称" :label-width="formLabelWidth">
          <el-input v-model="business.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="详情地址" :label-width="formLabelWidth">
          <el-select v-model="business.address" :multiple-limit="1" filterable remote reserve-keyword placeholder="请输入地址" :remote-method="remoteMethod" :loading="loading">
            <el-option v-for="item in options" :key="item.id" :label="item.cityname" :value="item.cityname"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="店铺介绍" :label-width="formLabelWidth">
          <el-input v-model="business.textInfo" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" :label-width="formLabelWidth">
          <el-input v-model="business.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺分类" :label-width="formLabelWidth">
          <el-cascader v-model="oldSelect" :options="optionsClass" @change="updateClass"></el-cascader>
        </el-form-item>
        <el-form-item label="店铺图片" :label-width="formLabelWidth">
          <el-upload action="http://127.0.0.1:8000/admin/Upload/addFile" :limit="1" ref="preupload" list-type="picture-card" :auto-upload="true" :on-success="succUpload">
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{ file }">
              <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
            </div>
            <el-image style="width: 145px; height: 145px; position: absolute; left: 158px" :src="business.busImg" fit="cover"></el-image>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelUpdate">取 消</el-button>
        <el-button type="primary" @click="sureUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import heads from "@/components/adminHead.vue";

import { selectAllBus, selectAddress, selectBusineClass, deleteFile, updateBusine, deleteBus } from "@/api/getPostApi";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {
    heads,
  },
  data() {
    //这里存放数据
    return {
      oldSelect: [],
      optionsClass: [],
      loading: false,
      dialogFormVisible: false,
      tableData: [],
      options: [],
      page: 1,
      pageSum: 0,
      formLabelWidth: "120px",
      business: {
        id: "",
        name: "",
        address: [],
        textInfo: "",
        phone: "",
        classId: "",
        sonClassId: "",
        busImg: "",
        newFilePath: "",
      },
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    addPage() {
      this.page += 1;
      this.selectBusList();
    },
    reducePage() {
      this.page -= 1;
      this.selectBusList();
    },
    updatePage(val) {
      this.page = val;
      this.selectBusList();
    },
    selectBusList() {
      selectAllBus(this.page).then((response) => {
        this.tableData = response.data.busList;
        this.pageSum = Math.ceil(response.data.count / 10);
      });
    },
    uodateBus(id) {
      this.dialogFormVisible = true;
      this.tableData.forEach((value) => {
        if (value.id === id) {
          this.business.id = value.id;
          this.business.name = value.businessName;
          this.business.address = value.businessAddress;
          this.business.textInfo = value.businessText;
          this.business.phone = value.businessPhone;
          this.business.classId = value.businessClassId;
          this.business.sonClassId = value.buSecondClass;
          this.oldSelect = [`${value.businessClassId}`, `${value.buSecondClass}`];
          this.business.busImg = `http://127.0.0.1:8000/${value.businessImage}`;
        }
      });
    },
    remoteMethod(val) {
      this.loading = true;
      selectAddress(val).then((response) => {
        this.loading = false;
        this.options = response.data;
      });
    },

    cancelUpdate() {
      this.dialogFormVisible = false;
      this.$refs.preupload.uploadFiles = [];
      if (this.business.newFilePath) {
        deleteFile(this.business.newFilePath);
      }
      this.$message("您取消了修改");
    },

    succUpload(val) {
      this.business.newFilePath = val;
    },

    updateClass() {
      const [val, valT] = this.oldSelect;
      this.business.classId = val;
      this.business.sonClassId = valT;
      console.log(this.business.classId, this.business.sonClassId);
    },

    sureUpdate() {
      this.dialogFormVisible = false;
      if (!this.business.newFilePath) {
        this.business.newFilePath = this.business.busImg;
      }
      updateBusine(this.business.id, this.business.name, this.business.address, this.business.textInfo, this.business.phone, this.business.newFilePath, this.business.classId, this.business.sonClassId).then((response) => {
        if (response.data.status === 200) {
          this.$message({
            message: "恭喜你，修改成功",
            type: "success",
          });
        } else {
          this.$message.error("错了哦,修改失败");
        }
      });
      this.selectBusList();
    },
    deleteBus(id) {
      this.tableData.forEach((value, index) => {
        if (value.id === id) {
          this.tableData.splice(index, 1);
        }
      });

      deleteBus(id).then((response) => {
        if (response.data.status === 200) {
          this.$message({
            message: "恭喜你，删除成功",
            type: "success",
          });
        } else {
          this.$message.error("错了哦,删除失败");
        }
      });
      if (this.tableData <= 0) {
        this.reducePage();
        this.pageSum -= 1;
      }
    },
    addFoods(id) {
      this.$router.push({
        name: "addFoodstuff",
        params: {
          busId: id,
        },
      });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.selectBusList();
    selectBusineClass().then((response) => {
      this.optionsClass = response.data;
    });
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="scss" scoped>
//@import url(); 引入公共css类

@import "@/style/mixin.scss";
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  @include marginWidth;
  margin-right: 0;
  margin-bottom: 0;
}

footer {
  @include footer;
}
</style>
